package com.systop.controller;

import com.systop.pojo.Identification;
import com.systop.pojo.User;
import com.systop.service.UserService;
import com.systop.unti.Md5Utils;
import com.systop.unti.ResponseResult;
import org.apache.commons.io.FilenameUtils;
import org.apache.ibatis.annotations.Select;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    //用户登陆
    @RequestMapping(value = "/login")

    public ResponseResult login(@RequestParam("user_name") String user_name, @RequestParam("user_password")String user_password){
        user_password = Md5Utils.MD5(user_password);
        return userService.login(user_name,user_password);
    }
    //用户注册
    @RequestMapping(value = "/addUser")
    public ResponseResult addUser(@RequestParam("user_name") String user_name, @RequestParam("user_password")String user_password,
                                  @RequestParam("user_sex") Integer user_sex, @RequestParam("user_phone") Integer user_phone,
                                  @RequestParam("user_address") String user_address, @RequestParam(value = "file", required = false) MultipartFile[] file,
                                  @RequestParam("user_status") Integer user_status,@RequestParam("remark") String remark)throws IOException, JSONException{
        //查询数据库是否有此用户名
        if (userService.selUseName(user_name).getData() != null){
            //若有则返回空
            return null;
        }
        //没有则继续添加
        JSONObject res = new JSONObject();
        JSONObject resUrl = new JSONObject();
        List<String> imageurls=new ArrayList<>();
        for (MultipartFile files:file){
                String filename = UUID.randomUUID().toString().replaceAll("-", "");
                String ext = FilenameUtils.getExtension(files.getOriginalFilename());
                String filenames = filename + "." + ext;
                String pathname = "D:\\workspeace\\hgtt\\user\\src\\main\\resources\\pic\\" + filenames;
                String user_photo = "pic/" + filenames;
                files.transferTo(new File(pathname));
                User user = new User();
                user_password = Md5Utils.MD5(user_password);
                user.setUser_name(user_name);
                user.setUser_password(user_password);
                user.setUser_sex(user_sex);
                user.setUser_phone(user_phone);
                user.setUser_address(user_address);
                user.setUser_photo(user_photo);
                user.setUser_status(user_status);
                user.setRemark(remark);
                return userService.addUser(user);
        }
        return ResponseResult.build(500,"失败");
    }
    //根据id查询用户
    @RequestMapping(value = "/selUSerByid")
    public ResponseResult selUserByid(@RequestParam("user_id") Integer user_id){
        return userService.selUserByid(user_id);
    }
    //用户修改基本信息（不修改密码）
    @RequestMapping(value = "/updUser")
    public ResponseResult updUser(@RequestParam("user_name") String user_name, @RequestParam("user_password")String user_password,
                                  @RequestParam("user_sex") Integer user_sex, @RequestParam("user_phone") Integer user_phone,
                                  @RequestParam("user_address") String user_address, @RequestParam(value = "file", required = false) MultipartFile[] file,
                                  @RequestParam("user_status") Integer user_status,@RequestParam("remark") String remark,
                                  @RequestParam("user_id") Integer user_id) throws IOException {
        JSONObject res = new JSONObject();
        JSONObject resUrl = new JSONObject();
        List<String> imageurls=new ArrayList<>();
        for (MultipartFile files:file){
            String filename = UUID.randomUUID().toString().replaceAll("-", "");
            String ext = FilenameUtils.getExtension(files.getOriginalFilename());
            String filenames = filename + "." + ext;
            String pathname = "D:\\workspeace\\hgtt\\user\\src\\main\\resources\\pic\\" + filenames;
            String user_photo = "pic/" + filenames;
            files.transferTo(new File(pathname));
            User user = new User();
//            user_password = Md5Utils.MD5(user_password);
            user.setUser_name(user_name);
            user.setUser_password(user_password);
            user.setUser_sex(user_sex);
            user.setUser_phone(user_phone);
            user.setUser_address(user_address);
            user.setUser_photo(user_photo);
            user.setUser_status(user_status);
            user.setRemark(remark);
            user.setUser_id(user_id);
            return userService.updUser(user);
        }
        return ResponseResult.build(500,"失败");
    }
    //用户修改密码（多了一步验证）
    @RequestMapping(value = "/updUserPass")
    public ResponseResult updUserPass(@RequestParam("user_name") String user_name, @RequestParam("user_password")String user_password,
                                      @RequestParam("user_sex") Integer user_sex, @RequestParam("user_phone") Integer user_phone,
                                      @RequestParam("user_address") String user_address, @RequestParam(value = "file", required = false) MultipartFile[] file,
                                      @RequestParam("user_status") Integer user_status,@RequestParam("remark") String remark,
                                      @RequestParam("user_id") Integer user_id,@RequestParam("new_password") String new_password) throws IOException {
        user_password = Md5Utils.MD5(user_password);
        if (userService.login(user_name, user_password).getData() == null){
            return null;
        }
        for (MultipartFile files:file){
            String filename = UUID.randomUUID().toString().replaceAll("-", "");
            String ext = FilenameUtils.getExtension(files.getOriginalFilename());
            String filenames = filename + "." + ext;
            String pathname = "D:\\workspeace\\hgtt\\user\\src\\main\\resources\\pic\\" + filenames;
            String user_photo = "pic/" + filenames;
            files.transferTo(new File(pathname));
            User user = new User();
            new_password = Md5Utils.MD5(new_password);
            user.setUser_name(user_name);
            user.setUser_password(new_password);
            user.setUser_sex(user_sex);
            user.setUser_phone(user_phone);
            user.setUser_address(user_address);
            user.setUser_photo(user_photo);
            user.setUser_status(user_status);
            user.setRemark(remark);
            user.setUser_id(user_id);
            return userService.updUser(user);
        }
        return ResponseResult.build(500,"失败");
    }
    //用户认证
    @RequestMapping(value = "/selUs")
    public ResponseResult selUs(@RequestParam("school_data_name") String school_data_name,@RequestParam("school_data_num") String school_data_num,
                                @RequestParam("school_data_college") String school_data_college,@RequestParam("identification_user_id") Integer identification_user_id){
        if (userService.selUs(school_data_name, school_data_num, school_data_college).getData() == null){
            return null;
        }
        Identification identification = new Identification();
        identification.setIdentification_user_id(identification_user_id);
        identification.setIdentification_id(identification_user_id);
        identification.setIdentification_student_name(school_data_name);
        identification.setIdentification_student_num(school_data_num);
        identification.setIdentification_school_name(school_data_college);
        return userService.addIdent(identification);
    }
}
